clear all
set more off, perm
global path "C:\Users\micha\Dropbox\Pills JFQA\Replication Package JFQA"
set sortseed 1234567 // to ensure exact replicability - very small differences in coefficients may arise if sortseed is not fixed

* ####################################
* ### Source files:
* ####################################
qui {
* 1_CompustatCRSP - main dataset with Compustat data; 452 239 observations, 1980-2019, 37 145 unique GVKEYs
* 2-1_Spamann_state_inc - Spamann incorporation data - main dataset, downloaded from https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/KBPZ5V
* 2-2_Spamann_cik_cusip_crosswalk - Spamann incorporation data - additional crosswalk between CIK and CUSIPs, when CIK missing
* 3_state_laws - dates of introduction of BCL, CSL, FPL, DDL, PPL, based on Karpoff and Wittry (2018)
* 4_Compustat_forfirmage - auxiliary Compustat file, going back to 1950, to calculate firm "Age" (i.e. number of years the firm is a public firm))
* 5_CRSP - additional CRSP data with permno, historical SICs, share class etc.
* 6-1_ExcessReturns4f - Excess returns calculated using a separate code - 4 factor model
* 7_TotalQ.dta - Intangible capital to calculate Total Q, as in Peters and Taylor (2017)
* 81-810 incorporations SAS files - Compact Disclosure data for historical incorporations for 1988-1997
* 9_Incorporations_CRSP_Historical.dta - CRSP Historical Incorportions 
* 10-1_ppill1_ISS - ISS poison pills data, 
* 10-2_ppill2_CLS_JFE2017 - Cremers, Litov, Sepe (JFE, 2017) pill data
* 10-3_SDC_panel_generated - SDC pill data
* 11_InstitutionalOwnership - % of institutional investors from 13F/Thomson Reuters
* 12_TakeoverData - Data with takeover bids and premiums
* 13_StaggeredBoards - Staggered Board data from Cremers, Litov, Sepe (2017)
* 14_ResearchQuotient - Research Quotient from WRDS
* 15_Firminnovation - patents data from Kogan et al. (2017, JPE), online at Noah Stoffman's website.
* 16_Forecasts - mean forecast error of analysts per firm year, from IBES
* 17_LargeCustomer - relative importance of biggest customer, based on Compustat
}

* ####################################
* ### Prepare auxiliary files to merge-in to the main Compustat dataset
* ####################################
qui {
/*
* Load and backfill Spamann's data
use "$path\Source Data\2-2_Spamann_cik_cusip_crosswalk", clear
duplicates drop cik, force
tempfile cikcusip
save `cikcusip', replace

use "$path\Source Data\2-1_Spamann_state_inc", clear
merge m:1 cik using `cikcusip', gen(merge_misscik) keepusing(gvkey)
drop if sub_1995 == "1"				// Drop if flagged
drop if filing_period==.			
drop if state=="US" | state=="PR"	// Keep only 50+1 US States

* Backfill years 1992-1995
gen year = year(filing_period)
gsort cik -filing_period
duplicates drop cik year, force
bysort cik: egen earliest_year = min(year)			// Determine the earliest filing for a given firm
gen years_to_expand = earliest_year-1991			// Determine how many copies of earliest observation needed
expand years_to_expand if year==earliest_year, gen(backfill)	// Add required duplicates of the first filing
gsort cik -year +backfill
by cik: replace year = year[_n-1]-1 if backfill==1	// Replace years of the first filing with appropriate earlier year
replace filing_period = mdy(month(filing_period),day(filing_period),year)	// Assume the same filing date, just different year
sort cik year
rename state inc_state
rename gvkey gvkeyn
gen fyear = year
save "$path\Created Datasets\Spamann_tomerge", replace
keep if merge_misscik==3
save "$path\Created Datasets\Spamann_tomerge_gvkey", replace

* Prepare firm age based on auxiliary Compustat data - 
use gvkey fyear using "$path\Source Data\4_Compustat_forfirmage", clear
duplicates drop gvkey fyear, force
sort gvkey fyear
by gvkey: gen firmage = _n
save "$path\Created Datasets\Firmage_tomerge", replace

* Prepare CRSP auxiliary data
use "$path\Source Data\5_CRSP_data", clear
gen fendyear = year(date)
gen fendmonth = month(date)
gen fbegyear = fendyear	// We will distinguish betwee beginning and end of fiscal year when merging to Compustat, but for CRSP data there is just one date
gen fbegmonth = fendmonth
duplicates drop permno fendyear fendmonth, force
rename permno lpermno
save "$path\Created Datasets\CRSP_tomerge", replace

* Prepare Excess returns files
use "$path\Source Data\6-1_ExcessReturns4f", clear
duplicates drop lpermno datadate, force
save "$path\Created Datasets\ExcessReturns4f_tomerge", replace

* Prepare Compact Disclosure Files
foreach filename in 5_incorporations011992 6_incorporations011993 7_incorporations011994 8_incorporations011995 9_incorporations011996 10_incorporations011997 {
	import sas using "$path\Source Data\8`filename'.sas7bdat", case(lower) clear
	keep year cusip correctcusip locincorp discconum coname
	save "$path/Created Datasets/8`filename'", replace
}
use "$path/Created Datasets/85_incorporations011992.dta", clear
append using "$path/Created Datasets/86_incorporations011993.dta"
append using "$path/Created Datasets/87_incorporations011994.dta"
append using "$path/Created Datasets/88_incorporations011995.dta"
*append using "$path/Created Datasets/89_incorporations011996.dta"	// Spamann data highly reliable after 1995
*append using "$path/Created Datasets/810_incorporations011997.dta"
gen cusip6 = substr(correctcusip,1,6)
duplicates drop cusip6 year, force
gen fyear = year
save "$path\Created Datasets\CompactDisclosure_92-95", replace

foreach filename in 1_incorporations011988 2_incorporations021989 3_incorporations121989 4_incorporations011991 {
	import sas using "$path\Source Data\8`filename'.sas7bdat", case(lower) clear
	keep year discco coname incorp cusip correctcusip
	save "$path/Created Datasets/8`filename'", replace
}

forvalues year = 1980/1987 { // Prepare years 1980-1987 via backfiling from 01/1988 file, which contains records from 1985-1987
	use "$path/Created Datasets/81_incorporations011988.dta", clear
	replace year = `year'
	tempfile cd_backfill`year'
	save `cd_backfill`year'', replace
}

use "$path/Created Datasets/81_incorporations011988.dta", clear
append using "$path/Created Datasets/82_incorporations021989.dta"
append using "$path/Created Datasets/83_incorporations121989.dta"
append using "$path/Created Datasets/84_incorporations011991.dta"
forvalues year = 1987(-1)1980 {
	append using `cd_backfill`year''
}
gen cusip6 = substr(correctcusip,1,6)
duplicates drop cusip6 year, force
rename incorp incorp8091
gen fyear = year
save "$path\Created Datasets\CompactDisclosure_80-91", replace

* Prepare CRSP Historical Incorporations Data
use "$path\Source Data\9_Incorporations_CRSP_Historical.dta", clear
gen year = year(datadate)
duplicates drop gvkeyn year, force
cap gen fyear = year
save "$path\Created Datasets\CRSP_historicalincorp_tomerge", replace
*/
} 

* ####################################
* ### Load main Compustat data (only required variables) and merge-in auxiliary files
* ####################################
 {
/*
use fyear datadate conm cik gvkey lpermno cusip tic incorp state sale  linktype adrr fic sic sich txditc at lt dcvt pstkl pstkrv pstk seq ceq fyr fyrc csho prcc_f act lct dvc oiadp oibdp dltt dlc xint txt re ppent mib capx xrd ni dp ugi ib ibc emp   xintd ebit act re using "$path\Source Data\1_CompustatCRSP.dta", clear
destring cik, replace force
destring gvkey, gen(gvkeyn)
gen year = fyear
replace year = fyear+1 if fyr<=5

* Merge-in firm age
merge m:1 gvkey fyear using "$path\Created Datasets\Firmage_tomerge", nogen keep(1 3)

* Merge-in CRSP data with stock prices and headers
gen fendyear = year
gen fendmonth = fyr
gen fbegyear = year-1
gen fbegmonth = fyr
merge m:1 lpermno fbegyear fbegmonth using "$path\Created Datasets\CRSP_tomerge", gen(merge_CRSP) keepusing(prc) keep(1 3)
rename prc prc_lag
merge m:1 lpermno fendyear fendmonth using "$path\Created Datasets\CRSP_tomerge", nogen keep(1 3)

* Merge-in excess returns
merge m:1 lpermno datadate using "$path\Created Datasets\ExcessReturns4f_tomerge", nogen keep(1 3)
rename (estimate) (eret4f)

* Merge-in Intangible Capital data
merge m:1 gvkey datadate using "$path\Source Data\7_TotalQ", nogen keep(1 3)

* Merge-in Spamann incorporation data
merge m:1 cik year using "$path\Created Datasets\Spamann_tomerge", gen(merged_spamann1)  keep(1 3)
merge m:1 gvkeyn year using "$path\Created Datasets\Spamann_tomerge_gvkey", gen(merged_spamann2)  keep(1 3)
gen merge_spamann = merged_spamann1
replace merge_spamann = 3 if merged_spamann2 == 3

* Merge-in Compact Disclosure data
gen cusip6 = substr(ncusip,1,6)
merge m:1 cusip6 year using "$path\Created Datasets\CompactDisclosure_92-95", nogen keepusing(locincorp ) keep(1 3)
merge m:1 cusip6 year using "$path\Created Datasets\CompactDisclosure_80-91", nogen keepusing(incorp8091) keep(1 3)

* Merge-in CRSP historical incorporation data 
merge m:1 gvkeyn year using "$path\Created Datasets\CRSP_historicalincorp_tomerge", nogen keepusing(incorph state_historical) keep(1 3)

* At this point, following incorporation variables are in the data:
* - inc_state contains state of incorporation based on Spamann data, including backfilling (which is flagged with backfill variable)
* - locincorp contains state of incorporation based on 1992-1995 Compact Disclosure data
* - incorp8091 contains state of incorporation based on 1980-1991 Compact Disclosure data, which includes backfiling before 1987
* - incorph contains state of incorporation based on CRSP historical data

*** ################# CREATE MAIN MEASURE OF THE HISTORICAL STATE OF INCORPORATION + an alternative based on CD data
gen incs = inc_state // Start with Spamman data. 
gen flag = 0 if incs!=""	// Flag = 0 - original Spamann data, whenever not missing
replace flag = 1 if (backfill==1 & inc_state!=locincorp & inc_state!="") & locincorp!="" 
replace incs = locincorp if (backfill==1 & inc_state!=locincorp & inc_state!="") & locincorp!="" // Flag 1 - Replace with Compact Disclosure data if Spamann data was backfiled and in conflict with CD
replace flag = 2 if backfill==1 & inc_state==locincorp & locincorp!="" // Flag 2 = Spamann and Compact Disclosure data are the same
replace flag = 3 if locincorp!="" & incs==""
replace incs = locincorp if incs=="" & locincorp!=""	// Flag 3 - if incorporation data still missing, use Compact Disclosure historical if available 
replace flag = 4 if incorph!="" & incs==""
replace incs = incorph if incs=="" & incorph!=""	// Flag 4 - if incorporation data still missing, use CRSP historical if available



* Merge-in State Laws - main measure
merge m:1 incs using "$path/Source Data/3_state_laws", nogen keep(1 3) 
rename (CS BC FP DD PP) (csl bcl fpl ddl ppl)
rename incs incs1

* Merge-in State Laws - Spamann incorporation data only
gen incs = inc_state	// Spamann data only - alternative way of coding laws for Table 5
merge m:1 incs using "$path/Source Data/3_state_laws", nogen keep(1 3) 
rename (CS BC FP DD PP) (csl3 bcl3 fpl3 ddl3 ppl3)
rename incs incs3
rename incs1 incs

*** #################  DEFINE STATE LAW INDICATORS
foreach law in csl bcl fpl ddl ppl {
	gen `law'h1 = 0 if incs!=""
	replace `law'h1 = 1 if year>=year(`law')
}

foreach law in csl bcl fpl ddl ppl {
	gen `law'h3 = 0 if incs3!=""
	replace `law'h3 = 1 if year>=year(`law'3)
}

* Merge-in Poison Pills
merge m:1 gvkeyn fyear using "$path/Source Data/10-1_ppill1_ISS.dta", keepusing(pill) keep(1 3) nogen
rename pill ppill_iss
merge m:1 gvkeyn fyear using "$path/Source Data/10-2_ppill2_CLS_JFE2017.dta", keepusing(nppill) keep(1 3) nogen
rename nppill ppill_cls
merge m:1 cusip6 year using "$path/Source Data/10-3_SDC_panel_generated", keepusing(ppill_sdc ) keep(1 3) nogen
*merge m:1 gvkey fyear using "$path/Source Data/10-4_Catan_pills", keep(1 3) nogen // see line 648 for details on running the code with alternative pill definition

* Define visible pill based on different data sources. We start from ISS data and whenever it is missing, we supplement it with further data
*gen p_pill = ppill_catan
gen p_pill = ppill_iss
*replace p_pill = ppill_iss if p_pill==. // ISS pills data
replace p_pill = ppill_cls if p_pill==. // Cremers, Litov, Sepe (JFE, 2017) data
replace p_pill = ppill_sdc if p_pill==. // SDC panel data

* Merge-in institutional ownership data
merge m:1 gvkeyn fyear using "$path/Source Data/11_InstitutionalOwnership.dta", nogen keep(1 3)

* Merge-in takeover data
merge m:1 gvkeyn fyear using "$path/Source Data/12_TakeoverData.dta", nogen keep(1 3)

* Merge in staggered boards data
merge m:1 gvkeyn fyear using "$path/Source Data/13_StaggeredBoards", nogen keep(1 3) 

* Merge-in research quotient
preserve
	use "$path/Source Data/14_ResearchQuotient", clear
	duplicates drop gvkeyn fyear, force
	keep gvkeyn fyear *_lxrd
	tempfile rq
	save `rq', replace
restore
merge m:1 gvkeyn fyear using `rq', nogen keep(1 3) 

* Merge in patent data
merge m:1 lpermno year using "$path/Source Data/15_Firminnovation", nogen keep(1 3) 



*** ################# Sample Cleaning and Variable Generation
drop if sale<0 | sale==. // Drop missing & negative sales
drop if shrcd==30 | shrcd==31 // Drop ADR

* Dropping observations with current incorporations outside of US (historical incorporations don't merge with anti-takeover laws anyway, so no need to filter on incs - historical state of incorporation)
drop if incorp=="AS" | incorp=="PR" | incorp=="VI" | incorp=="TT" | incorp=="GU" 

* Supplement missing historical SIC codes in CRSP/Compustat with such from CRSP, as over 98% of such codes in CRSP/Compustat are missing before 1986
gen miss_sich = sich==. 
bysort year: summ miss_sich
replace sich = hsiccd if year>=1980 & year<=1986 & sich==.

* Define SIC codes of various granularity for current/historical codes
destring sic, replace
foreach code in hsiccd siccd sich sic {
	gen `code'1=floor(`code'/1000)
	gen `code'2=floor(`code'/100)
	gen `code'3=floor(`code'/10)
}

* Drop Financials & Regulated
drop if sich2==49 
drop if sich1==6

* Generate Fixed effects
gen division = ""
replace division = "New England" if state=="ME" | state=="NH" | state=="VT" | state=="MA" | state=="CT" | state=="RI"
replace division = "Middle Atlantic" if state=="NY" | state=="PA" | state=="NJ"
replace division = "South Atlantic" if state=="DE" | state=="MD" | state=="WV" | state=="VA" | state=="NC" | state=="SC" | state=="GA" | state=="FL" | state=="DC"
replace division = "East South Central" if state=="KY" | state=="TN" | state=="MS" | state=="AL"
replace division = "East North Central" if state=="WI" | state=="MI" | state=="IL" | state=="IN" | state=="OH"
replace division = "West North Central" if state=="MN" | state=="ND" | state=="SD" | state=="NE" | state=="IA" | state=="KS" | state=="MO"
replace division = "West South Central" if state=="OK" | state=="TX" | state=="AR" | state=="LA"
replace division = "Mountain" if state=="MT" | state=="ID" | state=="WY" | state=="NV" | state=="UT" | state=="CO" | state=="AZ" | state=="NM"
replace division = "Pacific" if state=="CA" | state=="OR" | state=="WA" | state=="AK" | state=="HI"

gen region = ""
replace region = "West" if division=="Pacific" | division=="Mountain"
replace region = "Midwest" if division=="West North Central" | division=="East North Central"
replace region = "Northeast" if division=="New England" | division=="Middle Atlantic"
replace region = "South" if division=="West South Central" | division=="East South Central" | division=="South Atlantic"

egen div_fyear = group(division fyear)
egen region_fyear=group(region fyear)
egen sic3h_fyear=group(sich3 fyear)
egen sic3c_fyear=group(sic3 fyear)
egen stathq_fyear=group(state fyear)
egen sic2h_fyear = group(sich2 fyear)

gen otc=0 // Define OTC indicators
replace otc=1 if linktype=="NU"|linktype=="NR"

* Calculate book equity based on Fama French, 1992, market value, and Q
replace txditc = 0 if txditc==.
gen be=at-lt-pstkl+txditc+dcvt if pstkl!=.
replace be=at-lt-pstkrv+txditc+dcvt if pstkl==. & pstkrv!=.
replace be=at-lt-pstk+txditc+dcvt if pstkl==. & pstkrv==. & pstk!=.
gen me = csho*prcc_f
gen q = (at-be+me)/at
gen totq = (at-be+me)/(at+k_int)
gen roa1 = oibdp/at
gen logsize = log(at)

* Drop duplicates
duplicates drop gvkeyn fyear, force

* Create lags
xtset gvkeyn fyear
cap drop Lp_pill
gen Lp_pill = L.p_pill
gen Lq = L.q

* Winsorize and create logarithms
winsor2 q totq eret4f roa1 at, cuts(5 95) suffix(_w5)
gen lq_w5=ln(q_w5)
gen logtotq_w5 = ln(totq_w5)

* Create variable for visible pill at the moment of PPL adoption
gen tadopt = 0
replace tadopt = 1 if ppl-365<=datadate & datadate<ppl 
gen ppill_atadopt_temp = p_pill if tadopt==1
bysort gvkeyn: egen ppill_atadopt = max(ppill_atadopt_temp)
bysort gvkeyn: egen ever_adopt = max(tadopt)
replace ppill_atadopt = 0 if ever_adopt==0
gen pplh_ppillatadopt = pplh1*ppill_atadopt
replace ppill_atadopt = 0 if ppill_atadopt==.
replace pplh_ppillatadopt = 0 if pplh_ppillatadopt==.

* Create Q at adoption and duration of the pill for Table 3 purposes
gen qatadoptt = q_w5 if tadopt==1
bysort gvkeyn: egen qatadopt = max(qatadoptt)
gen qbel1 = q<1
gen pplh_q1 = pplh1*(qatadopt<1 & qatadopt!=.)
xtset gvkeyn fyear
gen adoption = p_pill==1 & L.p_pill==0
by gvkeyn: egen min_fyear = min(fyear) if p_pill==1 
gen count_ppill = (fyear - min_fyear) + 1
gen ln_count_ppill = ln(1+count_ppill)
winsor2 ln_count_ppill, cuts(5 95)

save "$path/Created Datasets/PPL_toanalyze", replace
*/
}

* ####################################
* ### Perform the Analysis
* ####################################

******************************************
* Figure 1
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

* Indicators for first wave, second wave, ... states
gen fwpplh = 0
replace fwpplh = 1 if datadate>=ppl & year(ppl)<=1990
gen fwpplh_ind = year(ppl)<=1990

gen swpplh = 0
replace swpplh = 1 if datadate>=ppl & year(ppl)>=1995 & year(ppl)!=.
gen swpplh_ind = year(ppl)>=1995 & year(ppl)!=.

gen defirms=0
replace defirms=1 if incs=="DE"

gen othernonpplh=0
replace othernonpplh=1 if fwpplh_ind==0 & swpplh_ind==0 & incs!="DE" 
drop if incs=="BERMUD" | incs=="CANADA" | incs=="ICELAN" | incs=="PANAMA" | incs=="UNITED"

gen cat = 1
replace cat = 1 if fwpplh==1
replace cat = 2 if swpplh==1
replace cat = 3 if defirms==1
replace cat = 4 if othernonpplh==1

egen  qquart = xtile(q_w5), n(4) by(fyear)
gen qa1 = qquart==1
gen qa4 = qquart==4
egen  qmed = xtile(q_w5), n(2) by(fyear)


preserve
	collapse (mean) p_pill, by(fyear cat)
	keep if inrange(fyear,1982,2012)
	gen ma = .
	* M&A activity measured with percent of US public firms acquired from Doidge, Karolyi, Stulz (2017)
	replace ma = 3.73 if fyear==1982
	replace ma = 3.64 if fyear==1983
	replace ma = 4.23 if fyear==1984
	replace ma = 4.60 if fyear==1985
	replace ma = 5.33 if fyear==1986
	replace ma = 4.52 if fyear==1987
	replace ma = 5.92 if fyear==1988
	replace ma = 4.38 if fyear==1989
	replace ma = 3.35 if fyear==1990
	replace ma = 2.02 if fyear==1991
	replace ma = 2.29 if fyear==1992
	replace ma = 2.90 if fyear==1993
	replace ma = 3.87 if fyear==1994
	replace ma = 4.77 if fyear==1995
	replace ma = 5.69 if fyear==1996
	replace ma = 6.42 if fyear==1997
	replace ma = 7.44 if fyear==1998
	replace ma = 8.06 if fyear==1999
	replace ma = 8.56 if fyear==2000
	replace ma = 6.61 if fyear==2001
	replace ma = 4.11 if fyear==2002
	replace ma = 4.50 if fyear==2003
	replace ma = 5.05 if fyear==2004
	replace ma = 4.71 if fyear==2005
	replace ma = 5.53 if fyear==2006
	replace ma = 7.27 if fyear==2007
	replace ma = 4.81 if fyear==2008
	replace ma = 2.86 if fyear==2009
	replace ma = 4.82 if fyear==2010
	replace ma = 4.80 if fyear==2011
	replace ma = 4.73 if fyear==2012
	twoway (connected p_pill fyear if cat==1, msymbol("S") mcolor(blue) lpattern(dot) lcolor(black)) (connected p_pill fyear if cat==2 & fyear>=1995, msymbol("D") mcolor(green) lpattern(dash) lcolor(black)) (connected p_pill fyear if cat==3, msymbol("O") mcolor(orange) lpattern(".-") lcolor(black)) (connected p_pill fyear if cat==4, msymbol("T") mcolor(red) lpattern(solid) lcolor(black)) (line ma fyear if cat==1, lpattern(dash) lcolor(gray) yaxis(2)), legend(pos(12) rows(1) lab(1 "First Wave PPL") lab(2 "Second Wave PPL") lab(3 "Delaware") lab(4 "Other Non-PPL") lab(5 "M&A Activity")) xscale(range(1980(2)2012)) xlabel(#11) xtitle("Year") ytitle("Percentage of Firms with a Pill", axis(1))  ytitle("% of Public Firms Acquired", axis(2))
	graph export "$path/Results/Fig_1.png", replace
restore

}

******************************************
* Figure 2
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
cap drop adoption
xtset gvkeyn fyear
gen adoption = p_pill==1 & L.p_pill==0
reghdfe lq_w5 L5.adoption L4.adoption L3.adoption L2.adoption L.adoption adoption F.adoption F2.adoption F3.adoption F4.adoption F5.adoption if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(fyear,1992,2012), abs(gvkeyn div_fyear sic3h_fyear) cluster(gvkeyn)
regsave L5.adoption L4.adoption L3.adoption L2.adoption L.adoption adoption F.adoption F2.adoption F3.adoption F4.adoption F5.adoption, ci level(95)
gen idnum = (_n-6)*(-1)
sort idnum

twoway (connected coef idnum, msymbol(T) mcolor(green) lcolor(black) lpattern(solid)) (line ci_lower idnum, lpattern(dash) lcolor(gray))  (line ci_upper idnum, lpattern(dash)  lcolor(gray)), xtitle("Year relative to PPill adoption") ytitle("Coefficient for Ln(Q)") yline(0, lpattern(dot)lcolor(gray)) legend(order(1 "Point Estimate" 2 "95% Confidence Interval") pos(12) rows(1)) xlabel(#11)
graph export "$path/Results/Fig_2.png", replace	
}

*********************************************
* Figure 3
*******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
gen PPL_passage = pplh1==1 & L.pplh1==0
gen numswitches = F5.PPL_passage + F4.PPL_passage + F3.PPL_passage +F2.PPL_passage +F1.PPL_passage + PPL_passage + L.PPL_passage +L2.PPL_passage +L3.PPL_passage + L4.PPL_passage +L5.PPL_passage +L6.PPL_passage+ L7.PPL_passage
keep if numswitches == 0 | numswitches==1 | numswitches==. // drops 170 out of 282 968 observations for which PPL status changes more than once due to reincorporation, and hence inappropriate for event study.
xtset gvkeyn fyear

gen aroundswitch = F5.PPL_passage==1 | F4.PPL_passage==1 |  F3.PPL_passage==1 | F2.PPL_passage==1 | F.PPL_passage==1 | PPL_passage==1 | L.PPL_passage==1 | L2.PPL_passage==1 | L3.PPL_passage==1 |  L4.PPL_passage==1 | L5.PPL_passage==1 | L6.PPL_passage==1 | L7.PPL_passage==1
egen staten = group(state)
eststo: reghdfe lq_w5 F5.PPL_passage   F4.PPL_passage  F3.PPL_passage F2.PPL_passage PPL_passage  L.PPL_passage L2.PPL_passage L3.PPL_passage  L4.PPL_passage L5.PPL_passage L6.PPL_passage L7.PPL_passage  aroundswitch bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(staten fyear)
local ind = 0
foreach var in F5.PPL_passage   F4.PPL_passage  F3.PPL_passage F2.PPL_passage PPL_passage  L.PPL_passage L2.PPL_passage L3.PPL_passage  L4.PPL_passage L5.PPL_passage L6.PPL_passage L7.PPL_passage {
	local ind = `ind'+1
	local coeff`ind' = _b[`var']
	local se`ind' = _se[`var']
}
drop *
set obs 13
gen c = .
gen se = .
forvalues i=1/4 {
	replace c = `coeff`i'' if _n==`i'
	replace se = `se`i'' if _n==`i'
}
replace c = 0 if _n==5	// omitted category t-1
replace se = . if _n==5 
forvalues i=5/12 {
	replace c = `coeff`i'' if _n==`i'+1
	replace se = `se`i'' if _n==`i'+1
}
gen cil = c-se*1.96
gen ciu = c+se*1.96
gen obs = _n-6
twoway (scatter c obs, xline(-0.5, lpattern(dash) lcolor(red)) ) (rcap ciu cil obs), legend(off) xlabel(-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7) xtitle("Years Since PPL Passage") ytitle("Coefficient on Ln(Q)") title("Evolution of Ln(Q) around PPL Passage")
graph export "$path/Results/Fig_3.png", replace 

* Mean and medians for eventual adopters
*bysort incs: egen everadopt = max(pplh1)
*bysort everadopt: summ q_w5 if pplh1==0 & otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., d
}

******************************************
* Table 1 
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
keep if inrange(fyear,1992,2012) & otc==0 & (backfill==0|inrange(flag,1,4)) & Lp_pill!=.

gen rdsales = xrd/sale
gen intcap=k_int/at
sort gvkeyn fyear
gen pat_stock = 0
gen pat_nonmiss = npats
replace pat_nonmiss = 0 if npats==.
by gvkeyn: replace pat_stock = pat_stock[_n-1] + pat_nonmiss if _n>1
gen patents = log(1+pat_stock)
replace patents = 0 if patents==.
winsor2 rdsales intcap aggbeta_lxrd patents, cuts(5 95) suffix(_w)
xtile qquart = q_w5, nq(4)
forvalues i=1/4 {
	gen qq`i' = qquart==`i'
}


label variable p_pill "Poison Pill"
label variable ln_count_ppill_w "Ln(Pill Duration)"
label variable lq_w5 "Ln(Q)"
label variable q_w5 "Q"
label variable logtotq_w5 "Ln(Total Q)"
label variable eret4f_w5 "Excess Return"
label variable tkoverbid "Takeover Bid"
label variable acquireI "Acquired"
label variable tot_prem "Total Premium"
label variable prem_increase "Premium Increase"
label variable pplh1 "PPL"
label variable bclh1 "BCL"
label variable cslh1 "CSL"
label variable ddlh1 "DDL"
label variable fplh1 "FPL"
label variable rdsales_w "R&D/Sales"
label variable intcap_w "Intangible Capital"
label variable patents_w "Ln(1+Patents)"
label variable aggbeta_lxrd_w "Research Quotient"

eststo clear
eststo: estpost summ p_pill ln_count_ppill_w lq_w5 q_w5 logtotq_w5 eret4f_w5 tkoverbid acquireI tot_prem prem_increase pplh1 bclh1 cslh1 ddlh1 fplh1 rdsales_w intcap_w patents_w aggbeta_lxrd_w, d 
esttab using "$path/Results/Tab_1.csv", cell("mean(fmt(%5.3f) label(Mean)) sd(fmt(%5.3f) label(St. Dev)) p25(fmt(%5.3g) label(P25)) p50(fmt(%5.3g) label(Median)) p75(fmt(%5.3g) label(P75)) count(fmt(%9.0f) label(Obs))") replace noobs label 

}

******************************************
* Table 2 
******************************************
{

use "$path/Created Datasets/PPL_toanalyze", clear
keep p_pill lq_w5  eret4f_w5 roa1_w5 tkoverbid state year incs fyear
collapse (mean) p_pill lq_w5  eret4f_w5 roa1_w5 (mean) tkoverbid, by(incs fyear)
rename fyear year
rename incs state
rename (*_w5) (*)
tempfile statelev
save `statelev', replace

use "$path/Source Data/statedata_PPL_prediction.dta", clear
merge 1:1 state year using `statelev'

egen staten = group(state)
tsset staten year
rename ln_pop_ ln_pop
zval p_pill lq eret4f roa1 tkoverbid state_bid state_transvalue lngdppc pgdp pctrep unemploy ln_pop est_entry est_exit, prefix(st_) 
foreach var in  bclaw cslaw ddlaw fplaw pplaw st_p_pill st_lq  st_eret4f st_roa1 st_tkoverbid st_state_bid st_state_transvalue st_lngdppc st_pgdp st_pctrep st_unemploy st_ln_pop st_est_entry st_est_exit {
		gen `var'1 = l.`var'
}

gen nopplaw = pplaw1==0
stset year, id(staten) failure(pplaw)

xi i.year
 
eststo clear
cap eststo:  stcox bclaw1 cslaw1 fplaw1 ddlaw1 _I* if nopplaw==1 & year>=1992 & year<=2012,  cluster(staten) nohr
cap eststo:  stcox bclaw1 cslaw1 fplaw1 ddlaw1 st_p_pill1 st_lq1 st_eret4f1 st_roa1  _I* if nopplaw==1 & year>=1992 & year<=2012,  cluster(staten) nohr
cap eststo:  stcox bclaw1 cslaw1 fplaw1 ddlaw1 st_p_pill1 st_lq1 st_eret4f1 st_roa1 st_tkoverbid1 st_state_transvalue1 _I* if nopplaw==1 & year>=1992 & year<=2012, robust cluster(staten) nohr
cap eststo:  stcox bclaw1 cslaw1 fplaw1 ddlaw1 st_lngdppc1 st_pgdp1 st_pctrep1 st_unemploy1 st_ln_pop1 st_est_entry1 st_est_exit1 _I* if nopplaw==1 & year>=1992 & year<=2012, robust cluster(staten) nohr
// Full specification: ML estimation fails; eststo: stcox bclaw1 cslaw1 fplaw1 ddlaw1 st_lngdppc1 st_pgdp1 st_pctrep1 st_roa1 st_tkoverbid1 st_state_transvalue1  st_unemploy1 st_ln_pop1 st_est_entry1 st_est_exit1 st_p_pill1 st_lq1 st_eret4f1 _I* if nopplaw==1 & year>=1992 & year<=2012, robust cluster(staten) nohr

esttab using "$path/Results/Tab_2.csv", replace ar2 drop(*year*)

}

******************************************
* Table 3 
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

xtile qatad_quart = qatadopt, nq(4)
gen qa1 = qatad_quart==1
gen qa4 = qatad_quart==4
gen pplh_qa1 = pplh1*qa1
gen pplh_qa4 = pplh1*qa4
xtile qq = q_w5, nq(4)
gen q1 = qq==1
gen q4 = qq==4

xtset gvkeyn fyear
eststo clear
eststo: reghdfe p_pill pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe p_pill pplh1 q1 q4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
eststo: reghdfe p_pill pplh1 pplh_qa1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
eststo: reghdfe p_pill pplh1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
test pplh1 + pplh_qa1 - pplh1 - pplh_qa4 = 0
local testf = r(F)
estadd scalar ftest = `testf'
* Further part of the code adds the specification with the likelihood of takeover
eststo: reghdfe adoption pplh1 pplh_qa1 pplh_qa4  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012)  & L.p_pill==0, absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
test pplh1 + pplh_qa1 - pplh1 - pplh_qa4 = 0
local testf = r(F)
estadd scalar ftest = `testf'
eststo: reghdfe ln_count_ppill_w pplh1 pplh_qa1 pplh_qa4  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs  fyear)
test pplh1 + pplh_qa1 - pplh1 - pplh_qa4 = 0
local testf = r(F)
estadd scalar ftest = `testf'
esttab using "$path/Results/Tab_3.csv", keep(ppl* q1 q4) replace stats(N r2_a ftest) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}

******************************************
*Table 4
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
 
gen pplh_qatadopt  = pplh1*qatadopt
replace pplh_qatadopt = 0 if pplh_qatadopt==.
 
eststo clear
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 Lp_pill bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1  pplh_qatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
* Further part of the code adds the specification with the likelihood of takeover
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear region_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear stathq_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_4.csv", keep(pplh* Lp_pill) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)

}

******************************************
* Extension of Table 3, 4, OA13 - Takeover Likelihood
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
set sortseed 1234567 // to ensure exact replicability - very small differences in coefficients may arise if sortseed is not fixed
keep if year>=1992&year<=2012

merge m:1 cusip6 year using "$path/Source Data/10-3_SDC_panel_generated_with13d", keepusing(clearday ) keep(1 3) nogen
gen nonclearday = ppill_sdc
replace nonclearday = 0 if clearday==1


* Predictive model Cremers et al. (2009)
g ppe=ppent/at
g debt=dltt/at
g cash=act/at
g roa=oibdp/at
g mcap=abs(prc)*csho
gen lmcap = log(mcap)
winsor2 ppe debt cash roa mcap lmcap, cuts(1 99) 

bysort sic2 year: egen industry=max(tkoverbid)
replace industry=0 if industry==.
replace industry=1 if industry>0& industry!=.

duplicates drop gvkeyn year, force
xtset gvkeyn year
ppmlhdfe tkoverbid  ppe_w debt_w cash_w roa_w c.lmcap_w L.industry , absorb(year) d vce(cluster cik)
predict takeoverhat, xb
zval takeoverhat, prefix(z_)
gen tkvatadoptt = z_takeoverhat if tadopt==1
bysort gvkeyn: egen tkvatadopt = max(tkvatadoptt)
replace tkvatadopt = 0 if tkvatadopt==.
* Addendum to Table 3, column 6
eststo: reghdfe p_pill 1.pplh1##c.tkvatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
* Addendum to Table OA13, column 6
eststo: reghdfe nonclearday 1.pplh1##c.tkvatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

xtile tkvatadopt_quart = tkvatadopt if tkvatadopt!=0, nq(4)
gen tka1 = tkvatadopt_quart==1
gen tka4 = tkvatadopt_quart==4
gen pplh_tka1 = pplh1*tka1
gen pplh_tka4 = pplh1*tka4
* Addendum to Table 3, column 7
eststo: reghdfe p_pill pplh1 pplh_tka1 pplh_tka4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
* Addendum to Table OA13, column 7
eststo: reghdfe nonclearday pplh1 pplh_tka1 pplh_tka4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

gen pplh_tkvatadopt = pplh1*tkvatadopt
replace pplh_tkvatadopt = 0 if pplh_tkvatadopt==.
* Addendum to 4, column 5
eststo: reghdfe lq_w5 pplh1   pplh_tkvatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

}

******************************************
* Table 5 
******************************************
{
clear
use "$path/Created Datasets/PPL_toanalyze", clear
tsset gvkeyn fyear

* Matching
gen q1_w = L.q_w5
gen size = log(at_w5)
gen size1_w = L.at_w5
gen ppl_year = year(ppl)
gen lagpill = L.p_pill
gen weight_hold = .
gen size_match = .
gen qlag_match = .

foreach y in 1995 1998 1999 2001 2002 2003 2005 2008 2009 {
	preserve
	keep if fyear==`y' & (ppl_year==`y' | (ppl_year>`y'+5 & pplh1==0))	
	forvalues j=0/1 {
			cap noisily psmatch2 pplh1 q1_w size1_w if otc==0 & (backfill==0 | inrange(flag,1,4)) & lagpill==`j', n(5) 
			cap export delimited lpermno _id incs ppl _n1 _n2 _n3 _n4 _n5 using "$path/Created Datasets/matchids_`y'_pill`j'.csv", replace
			cap replace weight_hold = _weight if p_pill==`j'
	}
	replace size1_w = . if weight_hold==.
	replace q1_w = . if weight_hold==.
	rename weight_hold w`y'
	rename size1_w size_match`y'
	rename q1_w qlag_match`y'
	keep gvkeyn w`y' size_match`y' qlag_match`y'
	gen baseyear`y' = `y'
	tempfile weights`y'
	save `weights`y'', replace
	restore
}
foreach i in 1995 1998 1999 2001 2002 2003 2005 2008 2009  {
	merge m:1 gvkeyn using `weights`i'', nogen
}
egen matchedsample = rowtotal(w19* w20*)
foreach var in size_ qlag_ {
	egen `var'bal = rowmean(`var'match*)
}
egen baseyear = rowmin(baseyear*)
bysort pplh1: summ *bal size1_w  q1_w if matchedsample
//gen w = matchedsample>0 & matchedsample!=. * The original line of the code. With pseudo-data, it's difficult to generate enough matches, therefore line below artificially creates some matches
gen w = 1	// this line is not there when code run with original data
eststo clear
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 [fweight=w] if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=. & inrange(fyear,baseyear-3,baseyear+3), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 [fweight=w] if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=. & inrange(fyear,baseyear-3,baseyear+3), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 [fweight=w] if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=. & inrange(fyear,baseyear-3,baseyear+3), absorb(gvkey fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh3 bclh3 cslh3 ddlh3 fplh3 if inrange(year,1992,2012) & backfill==0, absorb(gvkey sic3h_fyear div_fyear) cluster(incs3 fyear)
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if inrange(year,1992,2012) & (backfill==0 | inrange(flag,1,4)), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
esttab using "$path/Results/Tab_5A.csv", keep(pp*) replace stats(N r2_a) star(* 0.1 ** 0.05 *** 0.01)


* Column 6 generated in a separate data-run. The data creation part of the code (lines 1-367) has to be re-run with folllowing changes:
* uncomment lines 233, 236, 238, 704
* comment lines 237
* Changes need to be undone, and lines 1-367 rerun if one wants to generate the rest of the tables with the main definition of the pill.
*eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)


* ### Panel B
* ### Callaway Sant'Anna  Approach
use "$path/Created Datasets/PPL_toanalyze", clear
eststo clear
g first_treat=0
replace first_treat=1986	if incs=="IN"
replace first_treat=1986	if incs=="OH"
replace first_treat=1987	if incs=="WI"
replace first_treat=1988	if incs=="ID"
replace first_treat=1988	if incs=="PA"
replace first_treat=1988	if incs=="GA"
replace first_treat=1988	if incs=="HI"
replace first_treat=1988	if incs=="KY"
replace first_treat=1988	if incs=="NY"
replace first_treat=1989	if incs=="OR"
replace first_treat=1989	if incs=="UT"
replace first_treat=1989	if incs=="CO"
replace first_treat=1989	if incs=="TN"
replace first_treat=1989	if incs=="IA"
replace first_treat=1989	if incs=="NC"
replace first_treat=1989	if incs=="NV"
replace first_treat=1989	if incs=="FL"
replace first_treat=1989	if incs=="NJ"
replace first_treat=1989	if incs=="MA"
replace first_treat=1989	if incs=="IL"
replace first_treat=1990	if incs=="SD"
replace first_treat=1990	if incs=="VA"
replace first_treat=1990	if incs=="RI"
replace first_treat=1995	if incs=="MN" //95 calendar time
replace first_treat=1998	if incs=="WA" //98 calendar time
replace first_treat=1998	if incs=="SC"
replace first_treat=1999	if incs=="MD" //99 calendar time
replace first_treat=1999	if incs=="MO"
replace first_treat=2001	if incs=="MI"
replace first_treat=2002	if incs=="ME" //02 calendar time
replace first_treat=2002	if incs=="TX" //02 calendar time
replace first_treat=2003	if incs=="CT"
replace first_treat=2005	if incs=="MS" //05 calendar time
replace first_treat=2008	if incs=="VT"
replace first_treat=2009	if incs=="WY" //09 calendar time

sort gvkeyn year, stable
by gvkeyn year: gen num=_n
keep if num==1
drop num

cap {
*csdid lq_w5 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., ivar(gvkeyn) time(year) gvar(first_treat) method(dripw) agg(event) window(-3,+3) saverif(rif_example) wboot replace rseed(1) notyet 
estat pretrend
estat cevent, window (-3,+3)
}

cap {
*csdid lq_w5 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., ivar(gvkeyn) time(year) gvar(first_treat) method(dripw) agg(event) window(-5,+5) saverif(rif_example) wboot replace rseed(1) notyet 
estat pretrend
estat cevent, window (-5,+5)
}

cap {
*csdid lq_w5 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., ivar(gvkeyn) time(year) gvar(first_treat) method(dripw) agg(event) window(-10,+10) saverif(rif_example) wboot replace rseed(1) notyet 
estat pretrend
estat cevent, window (-10,+10)
}


}

******************************************
*  Table 6
******************************************
{

use "$path/Created Datasets/PPL_toanalyze", clear
eststo clear
eststo: reghdfe q_w5 pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe logtotq_w5 pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe roa1_w5 pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe eret4f_w5 pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_6.csv", keep(pp*) replace stats(N r2_a) star(* 0.1 ** 0.05 *** 0.01)


}

******************************************
* 	Table 7 
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

eststo clear

*** 1: Intangible Capital 
xtset gvkeyn fyear
gen intcap=k_int_know/at
gen intcap_lag=L.intcap
winsor2 intcap intcap_lag, cuts(5 95) suffix(_w5)
gen icap_atintrot = intcap_lag if tadopt==1
bysort gvkeyn: egen icap_atintro = max(icap_atintrot)
egen z_icap_atintro = std(icap_atintro)
replace z_icap_atintro = 0 if z_icap_atintro == .
gen pplh_icap_atintro = pplh1*z_icap_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_icap_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** 2: Organizational Capital
cap drop orgcap
gen orgcap=k_int_org/at
gen orgcap_lag = L.orgcap
winsor2 orgcap_lag, cuts(5 95) suffix(_w5)
gen orgcap_atintrot = orgcap_lag_w5 if tadopt==1
bysort gvkeyn: egen orgcap_atintro = max(orgcap_atintrot)
egen z_orgcap_atintro = std(orgcap_atintro)
replace z_orgcap_atintro = 0 if z_orgcap_atintro==.
gen pplh_orgcap_atintro = pplh1*z_orgcap_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_orgcap_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** 3: R&D/Sales
sort gvkeyn fyear
gen rnd_stock = 0
gen xrd_nonmiss = xrd
replace xrd_nonmiss = 0 if xrd==.
by gvkeyn: replace rnd_stock = rnd_stock[_n-1]/1.1 + xrd_nonmiss if _n>1
xtset gvkeyn fyear
gen rnd = rnd_stock/at
gen rnd_lag = L.rnd
winsor2 rnd rnd_lag, cuts(5 95) suffix(_w5)
gen rnd_atintrot = rnd_lag if tadopt==1
bysort gvkeyn: egen rnd_atintro = max(rnd_atintrot)
egen z_rnd_atintro = std(rnd_atintro)
replace z_rnd_atintro = 0 if z_rnd_atintro==. 		
gen pplh_rnd_atintro = pplh1*z_rnd_atintro
replace pplh_rnd_atintro = 0 if pplh1==0
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_rnd_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** 4: Patents
sort gvkeyn fyear
gen pat_stock = 0
gen pat_nonmiss = npats
replace pat_nonmiss = 0 if npats==.
by gvkeyn: replace pat_stock = pat_stock[_n-1]/1.1 + pat_nonmiss if _n>1
gen patents = log(1+pat_stock)
replace patents = 0 if patents==.
xtset gvkeyn fyear
gen patents_lag = L.patents
winsor2  patents_lag, cuts(5 95) suffix(_w5)
gen pat_atintrot = patents_lag_w5 if tadopt==1
bysort gvkeyn: egen pat_atintro = max(pat_atintro)
egen z_pat_atintro = std(pat_atintro)
replace z_pat_atintro = 0 if z_pat_atintro == .
gen pplh_pat_atintro = pplh1*z_pat_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_pat_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** 5: Forecast Error
merge m:1 gvkey fyear using "$path/Source Data/16_Forecasts", nogen keep(1 3) 
sort gvkeyn fyear
winsor2 ForecastError, cuts(5 95) suffix(_w)
gen ferr_atintrot = L.ForecastError_w if tadopt==1
bysort gvkeyn: egen ferr_atintro = max(ferr_atintrot)
egen z_ferr_atintro = std(ferr_atintro)
replace z_ferr_atintro = 0 if z_ferr_atintro == .
gen pplh_ferr_atintro = pplh1*z_ferr_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_ferr_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** 6: Large Customer
merge m:1 gvkeyn fyear using "$path/Source Data/17_LargeCustomer", nogen keep(1 3) 
sort gvkeyn fyear
gen largecustomer = max_ratio>0.05 & max_ratio!=. 
gen largcus_atintrot = L.largecustomer if tadopt==1
bysort gvkeyn: egen largcus_atintro = max(largcus_atintrot)
replace largcus_atintro = 0 if largcus_atintro==.
gen pplh_largcus_atintro = pplh1*largcus_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_largcus_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** 7: Triple interaction of large customer and intangible capital
summ icap_atintrot, d
local highthres = r(p75)
gen highicap_atintrot = icap_atintrot>`highthres' & icap_atintrot!=.
bysort gvkeyn: egen highicap_atintro = max(highicap_atintrot)
replace highicap_atintro = 0 if highicap_atintro == .
gen pplh_highicap_atintro = pplh1*highicap_atintro
gen pplh_largcus_atintroXint = pplh_largcus_atintro*highicap_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_highicap_atintro pplh_largcus_atintro pplh_largcus_atintroXint bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** Footnote: Research Quotient
xtset gvkeyn fyear
gen rq = aggbeta_lxrd
gen rq_lag = L.rq
winsor2 rq_lag, cuts(5 95) suffix(_w5)
gen rq_atintrot = rq_lag_w5 if tadopt==1
bysort gvkeyn: egen rq_atintro = max(rq_atintrot)
egen z_rq_atintro = std(rq_atintro)
replace z_rq_atintro = 0 if z_rq_atintro==.
gen pplh_rq_atintro = pplh1*z_rq_atintro
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt pplh_rq_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

*** Panel B: Takeover analysis
encode division, gen(divisions)
winsor2 tot_prem prem_increase, cuts(5 95) suffix(_w)

eststo: reghdfe tkoverbid pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., a(sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe acquireI pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., a(sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe tot_prem_w pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., a(divisions sic3) cluster(incs fyear)
eststo: reghdfe prem_increase_w pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., a(divisions sic3) cluster(incs fyear)
eststo: reghdfe tkoverbid pplh1 pplh_ppillatadopt pplh_icap_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., a(sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe acquireI pplh1 pplh_ppillatadopt pplh_icap_atintro bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=., a(sic3h_fyear div_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_7.csv", keep(pp*) replace stats(N r2_a) star(* 0.1 ** 0.05 *** 0.01)


}

******************************************
* 	Figure OA1
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1  if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
regsave pplh1 using "$path/Created Datasets/dropST", replace ci
foreach st in MN SC WA MD MO MI ME CT TX MS VT WY {
	reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1  if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=. & incs!="`st'", absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
	regsave pplh1 using "$path/Created Datasets/dropST_`st'", replace ci
}
use "$path/Created Datasets/dropST", clear
foreach st in MN SC WA MD MO MI ME CT TX MS VT WY {
	append using "$path/Created Datasets/dropST_`st'"
	rm "$path/Created Datasets/dropST_`st'.dta"
}
rm "$path/Created Datasets/dropST.dta"
gen state = "-"
gen idnum = _n
local i=1
foreach st in MN SC WA MD MO MI ME CT TX MS VT WY {
	local i=`i'+1
	replace state = "`st'" if _n==`i'
}
labmask idnum, values(state)	// ssc install labutil if labmask not working

twoway (connected coef idnum, msymbol(T) mcolor(green) lcolor(black) lpattern(solid)) (line ci_lower idnum, lpattern(dash) lcolor(gray))  (line ci_upper idnum, lpattern(dash)  lcolor(gray)), xtitle("Excluded State") ytitle("Coefficient for Ln(Q)") yline(0, lpattern(dot)lcolor(gray)) legend(off) xlabel(#12, valuelabel)
graph export "$path/Results/Figure_OA1.png", replace
}

******************************************
* 	Figure OA2
******************************************
{
* Creating this figure  requires 1,000 iterations and takes considerable amount of time. For replication package the value of repetitions is set to 50 instead

local Nobs = 50 //1000
use "$path/Source Data/3_state_laws", clear
encode incs, gen(incscode)
keep incscode PP
tempfile laws
save `laws', replace

use "$path/Created Datasets/PPL_toanalyze", clear
merge m:1 incs using "$path/Source Data/3_state_laws", gen(mergeoriglaws) keep(1 3) 
bysort incs: egen hasppl = max(mergeoriglaws)
keep if mergeoriglaws==3
encode incs, gen(incscode)

reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
local coefforig = _b[pplh1]
local torig = _b[pplh1]/_se[pplh1]

forvalues i=1/`Nobs' {
	qui {
	cap drop code
	cap drop incscode
	cap drop incscoderaw
	cap drop PP
	bysort incs: gen code = runiform() if _n==1
	egen incscoderaw = rank(code)
	bysort incs: egen incscode = max(incscoderaw)
	merge m:1 incscode using `laws', nogen keepusing(PP)
	cap drop pplh1
	gen pplh1 = 0 if incscode!=.
	replace pplh1 = 1 if year>=year(PP)
	reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
	local coeff`i' = _b[pplh1]
	local t`i' = _b[pplh1]/_se[pplh1]
	}
	di "Iteration `i'"
}

drop *
set obs `Nobs'
gen coeff = .
gen t = .
forvalues i=1/`Nobs' {
	replace coeff = `coeff`i'' if _n==`i'
	replace t = `t`i'' if _n==`i'
}

hist coeff, xline(`coefforig') xtitle("Coefficient on PPL-Random") ytitle("Density")
graph export "$path/Results/Fig_OA2a.png", replace
hist t, xline(`torig') xtitle("T-statistic on PPL-Random") ytitle("Density")
graph export "$path/Results/Fig_OA2b.png", replace

}


******************************************
*Table OA1
******************************************
{
use "$path/Source Data/3_state_laws", clear
sort State
export delimited State PP using "$path/Results/Tab_OA1.csv", novarnames replace
}

******************************************
*Table OA2
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

xtile qatad_quart = qatadopt, nq(4)
gen qa1 = qatad_quart==1
gen qa4 = qatad_quart==4
gen pplh_qa1 = pplh1*qa1
gen pplh_qa4 = pplh1*qa4

eststo clear
bysort incs: egen eventual_swppl = max(pplh1)
gen eventual_qa1 = eventual_swppl*qa1
gen eventual_qa4 = eventual_swppl*qa4

cap eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & inrange(year,1983,1993) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs )
cap eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 eventual_swppl bclh1 cslh1 ddlh1 fplh1 if otc==0 & inrange(year,1983,1993) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs )
cap eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & inrange(year,1986,1993) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs )
cap eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 eventual_swppl bclh1 cslh1 ddlh1 fplh1 if otc==0 & inrange(year,1986,1993) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs )
cap eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & inrange(year,1989,1993) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs )
cap eststo: reghdfe p_pill pplh1 pplh_qa1 pplh_qa4 eventual_swppl bclh1 cslh1 ddlh1 fplh1 if otc==0 & inrange(year,1989,1993) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs )


esttab using "$path/Results/Tab_OA2.csv", keep(pplh* eventual*) star(* 0.1 ** 0.05 *** 0.01) replace ar2
}

******************************************
*Table OA3
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
replace pplh1 = 1 if fyear>1985 & fyear!=. & incs=="DE"
eststo clear
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=. & incs!="DE", absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=. & incs!="DE", absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_OA3.csv", keep(pplh1 pplh_ppillatadopt) star(+ 0.15 * 0.1 ** 0.05 *** 0.01) replace ar2
}

******************************************
*Table OA4
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

gen ppv = 0
replace ppv = 0.5 if fyear>=1985
replace ppv = 1 if fyear>=1985 & incs=="DE"	// Moran
replace ppv = 0 if inrange(fyear,1985,1989) & incs=="NJ"
replace ppv = 0 if inrange(fyear,1987,1988) & incs=="CO"
replace ppv = 0 if fyear==1989 & incs=="GA"
replace ppv = 1 if fyear>=1987 & incs=="MI"
replace ppv = 1 if fyear>=1987 & incs=="MN"
replace ppv = 1 if fyear>=1991 & incs!="NJ" & incs!="CO" & incs!="GA" // Georgia Pacific - weird, since the text says 1 for Maine after the case, but then also 1 for everyone else?
replace ppv = 2 if pplh1==1
replace ppv = 2 if fyear>=2003 & incs=="CT"
replace ppv = 2 if fyear>=1998 & incs=="GA"
replace ppv = 2 if fyear>=1999 & incs=="PA"
replace ppv = 2 if fyear>=2001 & incs=="VA"
gen ppv_pillatadopt = ppv*ppill_atadopt

eststo clear
eststo: reghdfe lq_w5 ppv bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 ppv ppv_pillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 ppv ppv_pillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear region_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 ppv ppv_pillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear stathq_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_OA4.csv", keep(ppv*) star(* 0.1 ** 0.05 *** 0.01) replace ar2

}

******************************************
* 	Table OA5
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear

gen sameinchq = incs==state
gen diffinchq = incs!=state
gen pplh1Xsameinchq = pplh1*sameinchq
gen pplh1Xdiffinchq = pplh1*diffinchq
eststo clear
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear stathq_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear stathq_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1Xsameinchq pplh1Xdiffinchq bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear stathq_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1Xsameinchq pplh1Xdiffinchq pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear stathq_fyear) cluster(incs fyear)
esttab using "$path/Results/Tab_OA5.csv", keep(*pplh*) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}

******************************************
* 	Table OA6 
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
eststo clear
eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=. & incs!="TX" & incs!="MD", absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4)) & inrange(year,1992,2012) & Lp_pill!=. & incs!="TX" & incs!="MD", absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_OA6.csv", keep(pplh*) replace stats(N r2_a) star(* 0.1 ** 0.05 *** 0.01)
}

* 	Table OA7 - Description of Variables Only, no results

******************************************
*    Table OA8
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
gen permno = .
replace permno=lpermno if lpermno!=.&permno==.
replace permno=npermno if npermno!=.&permno==.
rename year filing_year 
joinby permno filing_year using "$path/Source Data/fy_patents", unmatched(master)
drop _merge
joinby permno filing_year using "$path/Source Data/fy_patents_count", unmatched(master)
rename filing_year year

gen haspatents = patent_num_fy>0
replace haspatents = 0 if patent_num_fy == .
gen log_numpat = log(patent_num_fy)
gen log_cites = log(cites_sum_fy)
gen log_val = log(xi_nominal_sum_fy)

eststo clear
eststo: reghdfe D.haspatents pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe D.log_numpat pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe D.log_cites pplh1 pplh_ppillatadopt   bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe F2.D.haspatents pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe F2.D.log_numpat pplh1 pplh_ppillatadopt  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe F2.D.log_cites pplh1 pplh_ppillatadopt   bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
esttab, keep(pplh1 pplh_ppillatadopt) star(* 0.1 ** 0.05 *** 0.01) ar2

esttab using "$path\Results\Tab_OA8.csv", keep(*pplh* bclh1 cslh1 ddlh1 fplh1) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}

******************************************
*    Table OA9
******************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
eststo clear

cap drop post1989
gen post1989 = year>=1989

eststo: reghdfe lq_w5 pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1982,1992), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1##post1989 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1982,1992), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1982,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 pplh1 pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1989,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

esttab using "$path/Results/Tab_OA9.csv", keep(*pplh*) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}


*****************************************
* 	Table OA11
*****************************************
{
* Column 2 - Staggered Boards
eststo clear
use "$path/Created Datasets/PPL_toanalyze", clear
joinby gvkeyn fyear using "$path/Source Data/staggeredboard", unmatched(master) 
eststo: reghdfe lq_w5 1.pplh1##1.cbdefml  pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

* Columns 1, 3-6 - Golden PArachutes & Bylaws 
use "$path/Created Datasets/PPL_toanalyze", clear
joinby cik fyear using "$path/Source Data/iss19932009_v1", unmatched(master) 

gen supermaj_bylaws = labylw
gen supermaj_charters = lachtr
gen supermaj_merg = supermajor

eststo: reghdfe lq_w5 1.pplh1##i.goldenparachute pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 1.pplh1##1.supermaj_bylaws  pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 1.pplh1##1.supermaj_charters  pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 1.pplh1##1.supermaj_merg  pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

* Columns 7-8 - Effect on staggered board
use "$path/Created Datasets/PPL_toanalyze", clear
joinby gvkeyn fyear using "$path/Source Data/staggeredboard", unmatched(master) 
eststo: reghdfe cbdefml 1.pplh1  bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe cbdefml 1.pplh1  pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
esttab using "$path/Results/Tab_OA11.csv", keep(*pplh*) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}

*****************************************
* 	Table OA12
*****************************************
{
* Delta Interaction
use "$path/Created Datasets/PPL_toanalyze", clear
joinby gvkey fyear using "$path/Source Data/deltaveganew1.dta", unmatched(master) 
duplicates drop gvkey fyear, force
winsor2  log_delta, cuts(1 99) suffix(_w)
eststo clear
eststo: reghdfe lq_w5 1.pplh1##c.log_delta_w bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 1.pplh1##c.log_delta_w pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)

* Managerial Ownership
use "$path/Source Data/execucomp_xwnxzztnbfzygj8g", clear
rename *, lower
order *, sequential
tab ceoann
keep if ceoann=="CEO"
destring gvkey, replace force
rename gvkey gvkeyn
rename year fyear
keep fyear gvkeyn shrown_tot_pct
save "$path/Created Datasets/execucomp", replace

use "$path/Created Datasets/PPL_toanalyze", clear
joinby gvkeyn fyear using "$path/Created Datasets/execucomp", unmatched(master) 
replace shrown_tot_pct=0 if shrown_tot_pct==.
winsor2 shrown_tot_pct, cuts(1 99)
eststo: reghdfe lq_w5 1.pplh1##c.shrown_tot_pct_w bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe lq_w5 1.pplh1##c.shrown_tot_pct_w pplh_ppillatadopt bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0 | inrange(flag,1,4))  &  inrange(year,1992,2012) & Lp_pill!=., absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
esttab using "$path/Results/Tab_OA12.csv", keep(*pplh* *delta* *shrown*) replace stats(N r2_a) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}

*********************************************
* 	Table OA13
*********************************************
{
use "$path/Created Datasets/PPL_toanalyze", clear
merge m:1 cusip6 year using "$path/Source Data/10-3_SDC_panel_generated_with13d", keepusing(clearday ) keep(1 3) nogen

xtile qatad_quart = qatadopt, nq(4)
gen qa1 = qatad_quart==1
gen qa4 = qatad_quart==4
gen pplh_qa1 = pplh1*qa1
gen pplh_qa4 = pplh1*qa4
xtile qq = q_w5, nq(4)
gen q1 = qq==1
gen q4 = qq==4

xtset gvkeyn fyear


gen nonclearday = ppill_sdc
replace nonclearday = 0 if clearday==1

xtset gvkeyn fyear
eststo clear
eststo: reghdfe nonclearday pplh1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear)
eststo: reghdfe nonclearday pplh1 q1 q4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4))  & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
eststo: reghdfe nonclearday pplh1 pplh_qa1 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
eststo: reghdfe nonclearday pplh1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
eststo: reghdfe nonclearday pplh1 pplh_qa1 pplh_qa4 bclh1 cslh1 ddlh1 fplh1 if otc==0 & (backfill==0|inrange(flag,1,4)) & inrange(fyear,1992,2012), absorb(gvkey sic3h_fyear div_fyear) cluster(incs fyear )
test pplh1 + pplh_qa1 - pplh1 - pplh_qa4 = 0
local testf = r(F)
estadd scalar ftest = `testf'

esttab using "$path/Results/Tab_OA13.csv", keep(ppl* q1 q4) replace stats(N r2_a ftest) star(+ 0.15 * 0.1 ** 0.05 *** 0.01)
}


